//! \file WindowCoins.cpp
//! \brief TODO: Document
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------

#include "Elements.h"
#include "GuiCEGUI.h"
#include <stddef.h>
#include <DnD.h>

#include "Dice.h" // TODO: remove once rolled up to the DD library.
#include "Core.h"

using namespace CEGUI;

// =====[ Local Definitions ]===================================================

// =====[ Local DataTypes ]=====================================================

// =====[ Local Variables ]=====================================================

// =====[ Local Functions ]=====================================================

// =====[ Private (Internal) Methods ]==========================================

// =====[ Local Implementation ]================================================


// =============================================================================
// FxName
// -----------------------------------------------------------------------------
//! TODO: document
//!
//! \param [in] Arg
//!   TODO: document
//!
//! \retval Exception
//!   The success status of the requested operation.
//!   - eOKAY: Execution occurred as requested.
//!   .
//!
//! \b Example: \code
//! FxName(0);
//! \endcode
// -----------------------------------------------------------------------------
WindowCoins::WindowCoins(CEGUI::Window *pParent)
{
  // If no parent was requested, we'll attach to the root.
  if (pParent == NULL)
    { pParent = GuiCEGUI::getRoot(); }

  // Store our parent for future reference.
  m_pParent = pParent;

  // Load the console window layout and add it to the parent.
  pParent->addChildWindow(GuiCEGUI::getWm()->loadWindowLayout("dq_coins.layout"));

  // TODO: stuff...

  Window* pWindow = static_cast<Window*>(GuiCEGUI::getWm()->getWindow("Coins"));

  pWindow->setPosition(UVector2(UDim(0.05f, 0.0f), UDim(0.25f, 0.0f)));

//  Combobox* cbobox = static_cast<Combobox*>(GuiCEGUI::getWm()->getWindow("Dice/Combobox"));
//  // add items to the combobox list
//  cbobox->addItem(new MyListItem("1d6"));
//  cbobox->addItem(new MyListItem("1d8"));
//  cbobox->addItem(new MyListItem("1d10"));
//  cbobox->addItem(new MyListItem("1d12"));
//  cbobox->addItem(new MyListItem("1d20"));
//  cbobox->addItem(new MyListItem("1d100"));
//  cbobox->addItem(new MyListItem("1d6+1"));
//  cbobox->addItem(new MyListItem("1d6+2"));
//  cbobox->addItem(new MyListItem("1d8+1"));
//  cbobox->addItem(new MyListItem("1d8+2"));

  // Roll button.
  subscribeEvent("Coins/Button1", PushButton::EventClicked, SubscriberSlot(&eventButton1));
}


// TODO: document
bool WindowCoins::eventButton1(const CEGUI::EventArgs& Event)
{
//  int Value, Min, Max;
//  char Msg[100];
//
//  // Get the windows combobox.
//  Window *pRoot = GuiCEGUI::getRoot();
//  Window* pParent = static_cast<Window*>(pRoot->getChild("Dice"));
//  Combobox *pComboBox = static_cast<Combobox*>(pParent->getChild("Dice/Combobox"));
//
//  // Get the input text from the edit box.
//  std::string Text(pComboBox->getText().c_str());
//
//  // Get the values from the dice.
//  Value = mROLL(Text);
//  Min = mROLL_MIN(Text);
//  Max = mROLL_MAX(Text);
//
//  // Print the results to the console window.
//  sprintf(&(Msg[0]), "Min= %d | Value= %d | Max = %d", Min, Value, Max);
//  WindowConsole::getSingleton().appendText(&(Msg[0]));

//  static DnD::Coins *pCoins = NULL;

  String Text;
  static DnD::Class *pClass = NULL;

  // Create a player class if one doesn't exist.
  if (pClass == NULL)
    { pClass = new DnD::Class(DnD::eCLASS_BARBARIAN); }

  // Roll start gold for the player class.
  DnD::Coins Coins = pClass->rollStartGold();

  // Output the gold
  Window *pRoot = GuiCEGUI::getRoot();
  Window* pParent = static_cast<Window*>(pRoot->getChild("Coins"));

  // Copper
  Editbox *pEditbox = static_cast<Editbox*>(pParent->getChild("Coins/Copper"));
  pEditbox->setText(Coins.copperAsStr());

  // Silver
  pEditbox = static_cast<Editbox*>(pParent->getChild("Coins/Silver"));
  pEditbox->setText(Coins.silverAsStr());

  // Gold
  pEditbox = static_cast<Editbox*>(pParent->getChild("Coins/Gold"));
  pEditbox->setText(Coins.goldAsStr());

  // Platinum
  pEditbox = static_cast<Editbox*>(pParent->getChild("Coins/Platinum"));
  pEditbox->setText(Coins.platinumAsStr());

  // Weight
  pEditbox = static_cast<Editbox*>(pParent->getChild("Coins/Weight"));
  pEditbox->setText(Coins.weightAsStr());

  // Return the event was handled.
  return (true);
}



